/**
 * 
 */
package t4j.concurrency.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


class A{
	private final int id;
	
	public A(int id) {
		this.id = id;
	}

	public String toString() {return "A id:" + id;}
}

class Task implements Runnable{

	private testSync syncObj;
	private int count =0;
	
	public Task(testSync obj) {
		this.syncObj = obj;
	}
	
	@Override
	public void run() {
		try{
			while(!Thread.interrupted()){
				synchronized (this) {//会阻塞，而无法运行下一步
					//TimeUnit.SECONDS.sleep(2);
					wait(2000);
				}

				

				System.out.println("task " +new A(++count));
			
			}
		}catch(InterruptedException e){
			System.out.println("Task interrupted");
		}
		
	}
	
}

public class testSync {
	
	ExecutorService exec = Executors.newCachedThreadPool();

	A a ;
	
	Task t1 = new Task(this);
	
	public testSync() throws Exception {
		exec.execute(t1);
		exec.execute(t1);
		
		TimeUnit.SECONDS.sleep(10);
		
		exec.shutdownNow();
	}
	
	public static void main(String[] args) throws Exception {
	
		new testSync();	
	}
}
